package com.bootkafka.listener;

import com.bootkafka.SpringKafkaApplication;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

/**
 * @author lenovo
 */
@Component
@Slf4j
public class HelloConsumer {

    //监听一个或者多个Topic
    @KafkaListener(topics = SpringKafkaApplication.TOPIC_NAME)
    public void handler(String message, Acknowledgment ack) {
        try {
            System.out.println("收到消息：" + message);
            //确认收到消息
            ack.acknowledge();
        } catch (Exception exp) {
            // 如果处理失败，可以选择不确认或者记录日志等处理
            // 这里可以根据实际情况决定是否调用ack.nacknowledge()或不调用acknowledge()
            log.error("Error processing message", exp);
        }
    }
}

